package com.zqxq.lottery.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.bo.BaseBO;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.bo.sys.SysMsgCMSBO;
import com.zqxq.lottery.entity.o2o.sys.SysBannerO2O;
import com.zqxq.lottery.entity.o2o.sys.SysMsgO2O;
import com.zqxq.lottery.entity.po.sys.SysBannerPO;
import com.zqxq.lottery.entity.po.sys.SysMsgPO;
import com.zqxq.lottery.entity.vo.sys.SysBannerVO;
import com.zqxq.lottery.entity.vo.sys.SysMsgVO;
import com.zqxq.lottery.service.sys.ISysMsgService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@RequestMapping("/sysMsg")
public class SysMsgController extends SysBaseController{

    @Autowired
    private ISysMsgService sysMsgService;
    @Autowired
    private YmlConfig ymlConfig;

    @ApiOperation("获取分页列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysMsgVO>> queryPageList(@RequestBody SysMsgCMSBO baseBO){
        Result result = new Result();
        try{
            Page<SysMsgPO> page = new Page(baseBO.getPageNo(), baseBO.getPageSize());
            Wrapper wrapper = new EntityWrapper();
            if(baseBO.getId() != null){
                wrapper.eq("id", baseBO.getId());
            }
            if(baseBO.getStatus() != null){
                wrapper.eq("status", baseBO.getStatus());
            }
            if(baseBO.getType() != null){
                wrapper.eq("type", baseBO.getType());
            }
            wrapper.orderBy("sort", true);
            page = sysMsgService.selectPage(page, wrapper);
            Page<SysMsgVO> voPage = SysMsgO2O.toPageVO(page);
            result.setData(voPage);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Result save(@RequestBody SysMsgVO vo){
        Result result = new Result();
        try{
            SysMsgPO po = SysMsgO2O.toPO(vo);
            if(vo.getId() == null){
                Long id = ymlConfig.getPkId();
                po.setId(id);
            }
            sysMsgService.insertOrUpdate(po);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }
    @ApiOperation("修改")
    @PostMapping("/update")
    public Result updateMsg(){
        Result result = new Result();
        try{
            sysMsgService.updateMsg();
        } catch (Exception e){
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public Result deleteMsg(Long id){
        Result result = new Result();
        try{
            sysMsgService.deleteById(id);
        } catch (Exception e){
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

}
